<?php
namespace app\common\model\newmodel;

use think\Model;

class ProductOrderFlowRecord extends Model
{
    protected $table = 'product_order_flow_record';
    protected $pk = 'id';


    public function beforeFactory()
    {
        return $this->belongsTo('app\\common\\model\\newmodel\\Factory', 'before_factory_id', 'id');
    }

    public function afterFactory()
    {
        return $this->belongsTo('app\\common\\model\\newmodel\\Factory', 'after_factory_id', 'id');
    }

    //单个订单的转单记录
    public static function orderFlowRecordList($order_id='')
    {
        if(empty($order_id)){
            return ['code'=>false,'msg'=>'参数不完整'];
        }
        $res=self::with(['beforeFactory','afterFactory'])->where('order_id',$order_id)->order('flow_time','desc')->select()->toArray();
        return ['code'=>true,'msg'=>$res];
    }

    //添加转单记录
    public static function addOrderFlowRecord($data)
    {
        if(empty($data['after_factory_id']) || empty($data['before_factory_id']) || empty($data['order_id'])){
            return ['code'=>false,'msg'=>'参数不完整'];
        }
        $obj=new self();
        $obj->order_id=$data['order_id'];
        $obj->after_factory_id=$data['after_factory_id'];
        $obj->before_factory_id=$data['before_factory_id'];
        $obj->flow_reason=$data['flow_reason'];
        $obj->flow_time=time();
        $obj->save();
        return ['code'=>true,'msg'=>$obj];
    }
}